import os
from building import *

cwd = GetCurrentDir()

Import('asenv')
ASROOT = asenv['ASROOT']

objs = []

if(asenv['RELEASE']=='asboot'):
    asenv.Append(CPPDEFINES='configTOTAL_HEAP_SIZE=0x7000')
else:
    asenv.Append(CPPDEFINES='configTOTAL_HEAP_SIZE=0x800000')
asenv.Append(CPPDEFINES='MEMORY_SIZE=0x8000000')
asenv.Append(CPPDEFINES='SYSTEM_REGION_START=0x10000000')
asenv.Append(CPPDEFINES='SYSTEM_REGION_END=0x101f4000')
asenv.Append(CPPDEFINES='PAGE_SIZE=0x1000')
asenv.Append(CPPDEFINES='OS_TICKS_PER_SECOND=100')
asenv.Append(CPPDEFINES='USECONDS_PER_TICK=10000')
asenv.Append(CPPDEFINES='OS_STK_SIZE_SCALER=4')

if(asenv['RELEASE']=='asboot'):
    lds = '%s/../board.bcm2835/script/linker-boot.lds'%(cwd)
else:
    lds = '%s/../board.bcm2835/script/linker-app.lds'%(cwd)
if((asenv['MODULES'] != None) and ('RTTHREAD' in asenv['MODULES'])):
    lds = '%s/versatilepb.lds'%(cwd)
asenv.Append(LINKFLAGS='-static -T %s'%(lds))
asenv.Append(LINKFLAGS='-Map build/versatilepb/versatilepb.map')

MODULES = ['ECUM','SCHM','MCU',
           'DET','RINGBUFFER',
           'CAN','CANIF','PDUR','COM','COMM','CANTP','CANNM',
           'DCM','CANNM','CANSM','NM','OSEKNM','XCP',
           'SOAD','DOIP','LWIP','PCI',
           'J1939TP',
           ]

if(cwd.find('travis') != -1):
    # this is on travis, GUI is not possible as rely on QT to generate pictures to C source code
    pass
elif(os.getenv('SGAPP') != None):
    MODULES += ['LCD','GUI','SG','SG_DEMO_WATCH']

if((asenv['MODULES'] != None) and ('RTTHREAD' in asenv['MODULES'])):
    asenv.Append(CPPDEFINES='RT_HEAP_SIZE=0x800000')
    MODULES += ['LWEXT4']
    MODULES += ['FTP']
    MODULES += ['CLIB_STDIO_PRINTF']
    MODULES += ['CLIB_STDIO_CAN']
else:
    MODULES += ['FATFS','LWEXT4','VFS','DEV',
       'CLIB_ASHEAP','CLIB_MISCLIB',
       'CLIB_MBOX','CLIB_QSORT',
       'CLIB_STDIO_PRINTF','CLIB_STDIO_CAN',
       'FTP','SHELL',
       'EEP','FLS','EA','FEE','MEMIF','NVM',
       'DEM',
       'ASKAR'
       ]
    if(cwd.find('travis') != -1):
        MODULES.remove('FATFS')

ARCH='versatilepb'
arch='arm'

# post special process for asboot release
if(asenv['RELEASE']=='asboot'):
    asenv.Append(CPPDEFINES='FL_ERASE_PER_CYCLE=2048')
    if(os.getenv('MINIBLT') == 'YES'):
        MODULES = ['MCU','DET',
           'CAN','CANTP','DCM',
           'XCP','XCP_MINI',
           'FLASH',
           'PCI',
           'CLIB_ASHEAP','CLIB_MISCLIB',
           'CLIB_STDIO_PRINTF',
           'ANYOS',
           'MINIBLT',
           'CANTP_MINI','DCM_MINI'
           ]
    else:
        MODULES = ['ECUM','SCHM','MCU','DET',
               'CAN','CANIF','PDUR','CANTP',
               'DCM','XCP',
               'UIP','SOAD','DOIP','PROTOTHREAD',
               'FLASH',
               'SHELL','FLASH_CMD',
               'PCI',
               'CLIB_ASHEAP','CLIB_MISCLIB',
               'CLIB_STDIO_PRINTF',
               'TINYOS'
               ]
    lds = '%s/com/as.application/board.bcm2835/script/linker-flsdrv.lds'%(ASROOT)
    asenv['flsdrv'] = {'CPPDEFINES':['FLASH_DRIVER_DYNAMIC_DOWNLOAD',
                                  'FLS_START_ADDRESS=0x00040000',
                                  'FLS_END_ADDRESS=0x08000000',
                                  'FLASH_WRITE_SIZE=2'],
                       'CCFLAGS':[],
                       'CPPPATH':['%s/com/as.infrastructure/include'%(ASROOT)],
                       'LINKFLAGS':['-static','-T%s'%(lds)],
                       'objs':[]}

if(asenv['MODULES'] is None):
    asenv['MODULES'] = MODULES
else:
    MODULES = asenv['MODULES']

asenv['ARCH'] = ARCH
asenv['arch'] = arch

if('run' in COMMAND_LINE_TARGETS):
    qemu = Qemu()
    target = '%s/release/%s/versatilepb'%(ASROOT,asenv['RELEASE'])
    if(IsPlatformWindows()): target = target + '.exe'
    qemu.Run('-m 128 -M versatilepb -kernel %s'%(target))

asenv.Append(CPPPATH=['%s/../board.posix/common'%(cwd)])

if('MCU' in MODULES):
    objs += Glob('%s/../board.posix/common/Mcu*.c'%(cwd))

if('EEP' in MODULES):
    objs += Glob('%s/../board.posix/common/Eep*.c'%(cwd))

if('FLS' in MODULES):
    objs += Glob('%s/../board.posix/common/Fls*.c'%(cwd))

if('ANYOS' in MODULES):
    objs += Glob('%s/com/as.infrastructure/system/kernel/askar/portable/arm/startup.S'%(ASROOT))

Return('objs')
